import gnu.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Receiver {

	private CommPortIdentifier comPort = null;
	private SerialPort serialPort = null;
	private InputStream input = null;
	private OutputStream output = null;
	public String readMessage = "";

	public Receiver() {
	}

	public void connect(String str) throws PortInUseException,
			NoSuchPortException, UnsupportedCommOperationException,
			IOException, InterruptedException {
		System.out.println(str + " Start:");
		System.out.print("Com Port Identifying... ");
		comPort = CommPortIdentifier.getPortIdentifier(str);
		System.out.println("Done");
		System.out.print("Bluetooth Initializing... ");
		for (int x = 1; x <= 14; x++) {
			Thread.sleep(1000L);
			System.out.print(x + "... ");
		}
		System.out.println("Done");
		System.out.print("Serial Port Opening... ");
		serialPort = (SerialPort) comPort.open("forwarderInApp", 5000);
		System.out.println("Done");
		System.out.print("Serial Port Settings... ");
		portSettings(serialPort);
		System.out.println("Done");
		streamSetup();
	}

	private void portSettings(SerialPort port)
			throws UnsupportedCommOperationException {
		int baudRate = 115200;
		port.setSerialPortParams(baudRate, SerialPort.DATABITS_8,
				SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
	}

	public void streamSetup() throws IOException {
		output = serialPort.getOutputStream();
		input = serialPort.getInputStream();
	}

	public void disconnect() throws IOException {
		input.close();
		output.close();
		serialPort.close();
	}

	public String read() throws IOException {
		byte[] buffer = new byte[400];
		int available = input.available();
		String message = "No message";
		if (available > 0) {
			input.read(buffer);
			message = new String(buffer);
			input.read(buffer);
			return message;
		} else {
			return message;
		}
	}

	public static void main(String[] args) throws IOException,
			InterruptedException {
		System.out
				.println("------------------------------------------------------");
		System.out
				.println("------------------------------------------------------");
		System.out
				.println("------------------------------------------------------");
		System.out
				.println("----------------Bluetooth Connection------------------");
		System.out
				.println("------------------------------------------------------");
		System.out
				.println("------------------------------------------------------");
		System.out
				.println("------------------------------------------------------");
		String msg;
		Receiver receiver = new Receiver();
		try {
			System.out.println("Connecting... ");
			receiver.connect("COM7");
		} catch (PortInUseException e) {
			System.out.println("Failed");
			while (true) {
			}
		} catch (NoSuchPortException e) {
			System.out.println("Failed");
			while (true) {
			}
		} catch (UnsupportedCommOperationException e) {
			System.out.println("Failed");
			while (true) {
			}
		} catch (IOException e) {
			System.out.println("Failed");
			while (true) {
			}
		}
		System.out.println("Begin Reading... ");
		while (true) {
			msg = receiver.read();
			System.out.print("Received Message: ");
			System.out.println(msg);
		}
	}
}
